define(['router'], function () {
    var loginModule = angular.module('login', []);

    loginModule.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider.state({
            name: "login",
            url: "/login",
            templateUrl: "app/framework/login.html",
            controller: "loginCtrl"
        });
    }])
    loginModule.controller('loginCtrl', ['$scope', '$http', '$state', 'API_URL', 'COOKIES', '$rootScope', function ($scope, $http, $state, API_URL, cookie, $rootScope) {
        $rootScope.isLoginPage = true;
        $scope.username = "";
        $scope.password = "";

        $scope.login = function () {
            var user = {
                nickName: $scope.username,
                password: $scope.password
            };
            var req = {
                method: 'post',
                url: API_URL.USER_SERVER + "/core/user/login",
                data: user
            };
            $http(req).then(function (response) {
                if (response.data.code == 200) {
                    $rootScope.isLoginPage = false;
                    cookie.setCookie("LoginName", response.data.data.nickName);
                    cookie.setCookie("LoginID", response.data.data.id);
                    cookie.setCookie("UserName", response.data.data.userName);
                    $state.go("portal");
                } else {
                    alert("用户名或密码错误!")
                }
            });
        }

    }]);
    return loginModule;
});